package workers;

import java.util.concurrent.BlockingQueue;

import javax.swing.JOptionPane;

import model.Software;
import workerUIs.StepFrame;
import workerUIs.TestFrame;
import workflow.step.IntegrationStep;

public class IntegrationStepWorker extends AbstractStepSwingWorker {

	private final BlockingQueue<Software> inputImplQueue;
	private final BlockingQueue<Software> inputUIQueue;
	private final BlockingQueue<Software> outputTest;
	private TestFrame nextFrame;

	public IntegrationStepWorker(final StepFrame frame,
			final BlockingQueue<Software> inputImplQueue, final BlockingQueue<Software> inputUIQueue, final BlockingQueue<Software> outputTest, final TestFrame nextFrame) {
		super(frame);
		this.inputImplQueue = inputImplQueue;
		this.inputUIQueue = inputUIQueue;
		this.outputTest = outputTest;
		this.nextFrame = nextFrame;
	}

	@Override
	protected Software doInBackground() throws Exception {
		/**/
		getFrame().getNextButton().setEnabled(false);
		/**/
		this.software = this.inputImplQueue.take();
		this.software = this.inputUIQueue.take();
		new IntegrationStep().doStep(this.software);
		this.outputTest.put(this.software);
		/**/
		getFrame().getNextButton().setEnabled(true);
		/**/
		return this.software;
	}
	
	@Override
	protected void done() {
		super.done();
		if (inputImplQueue.isEmpty() || inputUIQueue.isEmpty()) {
			getFrame().getNextButton().setEnabled(false);
		}
		this.nextFrame.getNextButton().setEnabled(true);
		switch (JOptionPane.showConfirmDialog(this.nextFrame, "Tests succcessful?")) {
		case JOptionPane.YES_OPTION: 
			this.nextFrame.setTestSuccessful(true);
			break;
		default: 
			this.nextFrame.setTestSuccessful(false);
			break;
		}
	}

}
